package com.gongpingjialoan.activity.car;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/6/12.
 */
public class NewCarPriceActivity extends BaseActivity {

    private NetDataJson mNetDataJson;

    private String city, model_detail;

    private LoadingDialog loadingDialog;

    private String title, price, imageUrl, price_range;

    private ImageView mNetworkImageView;

    private TextView carNameTextView, newcarTextView, price_rangeTextView, cityTextView;

    private LayoutInflater mLayoutInflater;

    private LinearLayout dearsMain, dears;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_car_price_layout);
        setTitle("新车报价");
        mLayoutInflater = LayoutInflater.from(this);
        mNetworkImageView = (ImageView) findViewById(R.id.car_img);
        carNameTextView = (TextView) findViewById(R.id.carName);
        newcarTextView = (TextView) findViewById(R.id.newcar);
        price_rangeTextView = (TextView) findViewById(R.id.price_range);
        dearsMain = (LinearLayout) findViewById(R.id.delars_main);
        dears = (LinearLayout) findViewById(R.id.delars);
        cityTextView = (TextView) findViewById(R.id.city);
        dearsMain.setVisibility(View.GONE);

        city = getIntent().getStringExtra("city");
        model_detail = getIntent().getStringExtra("model_detail");
        title = getIntent().getStringExtra("title");
        price_range = getIntent().getStringExtra("price_range");
        price = getIntent().getStringExtra("price");
        imageUrl = getIntent().getStringExtra("image");
        cityTextView.setText(city + "4S店");
        ImageLoad.LoadImage(mNetworkImageView, imageUrl, R.drawable.car_loading, R.drawable.car_no, NewCarPriceActivity.this);
        carNameTextView.setText(title);
        newcarTextView.setText("新车指导价: " + price + "万");
        price_rangeTextView.setText(price_range);

        loadingDialog = new LoadingDialog(this);
        mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                loadingDialog.dismiss();
                Toast.makeText(NewCarPriceActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                loadingDialog.dismiss();
                JSONArray jsonArray;
                try {
                    jsonArray = json.getJSONArray("data");
                    int size = jsonArray.length();
                    if (size > 0) {
                        dearsMain.setVisibility(View.VISIBLE);
                        dears.removeAllViews();
                        for (int i = 0; i < size; i++) {
                            View view = mLayoutInflater.inflate(R.layout.newcar_price_item, null);
                            if (i % 2 == 0) {
                                view.setBackgroundColor(getResources().getColor(R.color.bg_grey));
                            } else {
                                view.setBackgroundColor(getResources().getColor(R.color.white));

                            }
                            TextView name = (TextView) view.findViewById(R.id.name);
                            TextView price = (TextView) view.findViewById(R.id.price);
                            String phone = jsonArray.getJSONObject(i).getString("phone");
                            ImageView imageView = (ImageView) view.findViewById(R.id.phone);
                            imageView.setTag(phone);
                            imageView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String cc = v.getTag().toString().replace(",", "");
                                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + cc));
                                    startActivity(intent);
                                }
                            });
                            name.setText(jsonArray.getJSONObject(i).getString("shop_name"));
                            price.setText("¥" + jsonArray.getJSONObject(i).getString("price") + "万");
                            dears.addView(view);
                        }
                    }

                } catch (JSONException e) {
                }

            }
        });
        mNetDataJson.setUrl(API.get_4soffer);
        mNetDataJson.addParam("model_detail", model_detail);
        mNetDataJson.addParam("city", city);
        mNetDataJson.request("get");
        loadingDialog.show();
    }
}
